﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Model;
using System.Configuration;
using Ext.Net;
using System.Data;
using Newtonsoft.Json;
using System.IO;
using System.Text;


namespace TCDC_System.Content.MasterData
{
    public partial class p_tcdc_mst_user : SystemPage.UIPage
    {
        private tcdc_mst_user objUser;
        protected void Page_Init(object sender, EventArgs e)
        {
            this.UserAdd1.GridStore = StoreUser;
        }

        protected void Page_Load(object sender, EventArgs e)
        {

            objUser = new tcdc_mst_user();
            objUser.strConn = ConfigurationManager.AppSettings["ConnString"];
            if (!X.IsAjaxRequest)
            {
                StoreUser.DataSource = GetUser();
                StoreUser.DataBind();
            }
        }

        protected DataTable GetUser()
        {
            DataTable dt = objUser.getByObject(new tcdc_mst_user());
            return dt;
        }

        protected void AddUser(object sender, DirectEventArgs e)
        {
            UserAdd1.Show();
        }

        protected void UpdateUser(object sender, DirectEventArgs e)
        {
            string json = e.ExtraParams["UserValue"];
            Dictionary<string, string>[] workingdays = JSON.Deserialize<Dictionary<string, string>[]>(json);
            string user_id = string.Empty;
            string user_type_id = string.Empty;
            string username = string.Empty;
            string password = string.Empty;
            string fname_th = string.Empty;
            string lname_th = string.Empty;
            string fname_en = string.Empty;
            string lname_en = string.Empty;
            string id_card = string.Empty;
            string birth_date = string.Empty;
            string face_image = string.Empty;
            string gender = string.Empty;
            string telephone = string.Empty;
            string mobile = string.Empty;
            string role = string.Empty;
            string lang = string.Empty;
            string create_date = string.Empty;
            string create_by = string.Empty;
            string update_date = string.Empty;
            string update_by = string.Empty;
            string site_id = string.Empty;

            foreach (Dictionary<string, string> row in workingdays)
            {
                user_id = row["user_id"].ToString();
                user_type_id = row["user_type_id"].ToString();
                username = row["username"].ToString();
                password = row["password"].ToString();
                fname_th = row["fname_th"].ToString();
                lname_th = row["lname_th"].ToString();
                fname_en = row["fname_en"].ToString();
                lname_en = row["lname_en"].ToString();
                id_card = row["id_card"].ToString();
                birth_date = row["birth_date"].ToString();
                face_image = row["face_image"].ToString();
                gender = row["gender"].ToString();
                telephone = row["telephone"].ToString();
                mobile = row["mobile"].ToString();
                role = row["role"].ToString();
                lang = row["lang"].ToString();
                create_date = row["create_date"].ToString();
                create_by = row["create_by"].ToString();
                update_date = row["update_date"].ToString();
                update_by = row["update_by"].ToString();
                site_id = row["site_id"].ToString();
            }

            objUser.user_id = Convert.ToInt32(user_id);
            objUser.user_type_id = Convert.ToInt32(user_type_id);
            objUser.username = username;
            objUser.password = password;
            objUser.fname_th = fname_th;
            objUser.lname_th = lname_th;
            objUser.fname_en = fname_en;
            objUser.lname_en = lname_en;
            objUser.id_card = id_card;
            objUser.birth_date = birth_date;
            objUser.face_image = face_image;
            objUser.gender = gender;
            objUser.telephone = telephone;
            objUser.mobile = mobile;
            objUser.role = role;
            objUser.lang = lang;
            objUser.create_date = create_date;
            objUser.create_by = Convert.ToInt32(create_by);
            objUser.update_date = update_date;
            objUser.update_by = Convert.ToInt32(update_by);
            objUser.site_id = Convert.ToInt32(site_id);
            objUser.updateData(objUser);
            StoreUser.DataSource = GetUser();
            StoreUser.DataBind();
        }

        protected void DeleteUser(object sender, DirectEventArgs e)
        {
            this.ConfirmDeleteUser();
        }

        protected void StoreUser_Refresh(object sender, StoreRefreshDataEventArgs e)
        {
            StoreUser.DataSource = GetUser();
            StoreUser.DataBind();
        }

        protected void ExportExcelUser(object sender, EventArgs e)
        {
            string json = GridData.Value.ToString();

            DataTable dt = (DataTable)JsonConvert.DeserializeObject(json, (typeof(DataTable)));

            if (dt.Rows.Count > 0)
            {
                Response.Clear();
                Response.AddHeader("content-disposition", "attachment;filename=User.xls");
                Response.ContentType = "application/vnd.ms-excel";
                StringWriter stringWriter = new StringWriter();
                HtmlTextWriter htmlWrite = new HtmlTextWriter(stringWriter);
                DataGrid dataExportExcel = new DataGrid();
                dataExportExcel.DataSource = dt;
                dataExportExcel.DataBind();
                dataExportExcel.RenderControl(htmlWrite);
                StringBuilder sbResponseString = new StringBuilder();
                sbResponseString.Append("<html xmlns:v=\"urn:schemas-microsoft-com:vml\" xmlns:o=\"urn:schemas-microsoft-com:office:office\" xmlns:x=\"urn:schemas-microsoft-com:office:excel\" xmlns=\"http://www.w3.org/TR/REC-html40\"> <head><meta http-equiv=\"Content-Type\" content=\"text/html; charset=utf-8\"></head> <body>");
                sbResponseString.Append(stringWriter + "</body></html>");
                sbResponseString.Replace("&nbsp;", "&#160;");
                Response.Write(sbResponseString.ToString());
                Response.End();
            }
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void ConfirmDeleteUser()
        {
            X.Msg.Confirm("Confirm", "ยืนยันการลบ ?", new MessageBoxButtonsConfig
            {
                Yes = new MessageBoxButtonConfig
                {
                    Handler = "MyNameSpace.DeleteDataUser()",
                    Text = "Yes"
                },
                No = new MessageBoxButtonConfig { Text = "No" }
            }).Show();
        }
        [DirectMethod(Namespace = "MyNameSpace")]
        public void DeleteDataUser()
        {
            RowSelectionModel sm = this.GridPanelUser.SelectionModel.Primary as RowSelectionModel;
            string user_id = string.Empty;

            foreach (SelectedRow row in sm.SelectedRows)
            {
                user_id = row.RecordID;
            }
            objUser.deleteData(user_id);
            StoreUser.DataSource = GetUser();
            StoreUser.DataBind();
        }
    }
}

